잠시만 기다려 주세요

     '입만 나불거리고 행동하지 않는 야당을 규탄한다.'
전체검색 :  
이번주 로또 및 연금번호 발생!!   |  HOME   |  여기는?   |  바다물때표   |  알림 (16)  |  여러가지 팁 (1054)  |  추천 및 재미 (150)  |  자료실 (22)  |  
시사, 이슈, 칼럼, 평론, 비평 (582)  |  끄적거림 (127)  |  문예 창작 (702)  |  바람 따라 (69)  |  시나리오 (760)  |  드라마 대본 (248)  |  
살인!


    golang

golang - golang 인터페이스 interface 예제, 설명
이 름 : 바다아이   |   조회수 : 7987         짧은 주소 : https://www.bada-ie.com/su/?991591780222
// 개인적으로 interface 를 잘 사용을 안 합니다. ^^;
// 프로그램을 어렵게 만들 수 있기 때문입니다. 그러나 꼭 필요한 곳이 생길 때가 있습니다. 피하고 싶으나 피할 수 없는 상황 -.-;
// 아래 예제 보시면서 어디다 쓰는 구나... 확인 하시면 되겠습니다. 감 오실 겁니다.


// _Interfaces_ are named collections of method
// signatures.

package main

import "fmt"
import "math"

// Here's a basic interface for geometric shapes.
type geometry interface {
	area() float64
	perim() float64
}

// For our example we'll implement this interface on
// `rect` and `circle` types.
type rect struct {
	width, height float64
}
type circle struct {
	radius float64
}

// To implement an interface in Go, we just need to
// implement all the methods in the interface. Here we
// implement `geometry` on `rect`s.
func (r rect) area() float64 {
	return r.width * r.height
}
func (r rect) perim() float64 {
	return 2*r.width + 2*r.height
}

// The implementation for `circle`s.
func (c circle) area() float64 {
	return math.Pi * c.radius * c.radius
}
func (c circle) perim() float64 {
	return 2 * math.Pi * c.radius
}

// If a variable has an interface type, then we can call
// methods that are in the named interface. Here's a
// generic `measure` function taking advantage of this
// to work on any `geometry`.
func measure(g geometry) {
	fmt.Println(g)
	fmt.Println(g.area())
	fmt.Println(g.perim())
}

func main() {
	r := rect{width: 3, height: 4}
	c := circle{radius: 5}

	// The `circle` and `rect` struct types both
	// implement the `geometry` interface so we can use
	// instances of
	// these structs as arguments to `measure`.
	measure(r)
	measure(c)
}


결과

{3 4}
12
14
{5}
78.53981633974483
31.41592653589793


출처 : https://gobyexample.com/interfaces
| |





      1 page / 6 page
번 호 카테고리 제 목 이름 조회수
179 golang golang , ... 바다아이 1307
178 golang golang , map . 바다아이 1043
177 golang Golang (, , data ) , ... 바다아이 1046
176 golang golang sort ... 바다아이 1215
175 golang golang html.EscapeString html.UnescapeString input value ... 바다아이 1385
174 golang golang go.mod go.sum . GOPATH SRC not module, 1.16 . 바다아이 4528
173 golang go 1.16 ... is not in GOROOT.. GOPATH .... . 바다아이 5364
172 golang , String Formatting 바다아이 6940
171 golang rand.Intn , random, , . 바다아이 6525
170 golang golang ... 바다아이 9277
169 golang golang gopath, goroot .. golang 바다아이 7112
168 golang golang ... Force download file example 바다아이 8874
167 golang golang , , cpu, memory, disk 바다아이 10160
166 golang golang , ... GOOS, GOARCH 바다아이 8046
165 golang golang checkbox ... 바다아이 7758
164 golang golang , , http .... 바다아이 7577
163 golang golang nil , nil , nil ... 바다아이 7843
162 golang 2 golang, go , .... golang .... 바다아이 10636
161 golang golang postgresql, mysql, mariadb ... ` Grave () .. .. 바다아이 8098
160 golang golang postgresql mysql, mariadb scan , null .. 바다아이 8198
159 golang golang , iconv 바다아이 10992
158 golang golang quote escape, unquote 바다아이 8365
157 golang golang , http errorLog , , ... 바다아이 8483
현재글 golang golang interface , 바다아이 7988
155 golang golang struct .... 바다아이 8657
154 golang golang map map , 바다아이 8224
153 golang golang map .... .... 바다아이 7744
152 golang golang slice copy 바다아이 7834
151 golang golang goto 바다아이 8673
150 golang golang slice sort , int, string, float64 바다아이 8102
| |









Copyright ⓒ 2001.12. bada-ie.com. All rights reserved.
이 사이트는 리눅스에서 firefox 기준으로 작성되었습니다. 기타 브라우저에서는 다르게 보일 수 있습니다.
[ Ubuntu + GoLang + PostgreSQL + Mariadb ]
서버위치 : 오라클 클라우드 춘천  실행시간 : 0.05596
to webmaster... gogo sea. gogo sea.